package com.att.action;

import com.adkfp.utils.LayuiUtil;
import com.att.bean.NumRunBean;
import com.att.bean.NumRunDeilBean;
import com.att.bean.SchClassBean;
import com.att.service.NumRunDeilService;
import com.att.service.NumRunService;
import com.att.service.SchClassService;
import com.att.service.UserOfRunDeilService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("numRunDeil")
public class NumRunDeilAction {

    @Resource(name = "numRunDeilService")
    private NumRunDeilService numRunDeilService;
    @Resource(name = "numRunService")
    private NumRunService numRunService;
    @Resource(name = "userOfRunDeilService")
    private UserOfRunDeilService userOfRunDeilService;

    /**
     * Description: 带条件查询人员排班时间段记录
     *
     * @param bean
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll(NumRunDeilBean bean) {
        List<List<NumRunDeilBean>> list = numRunDeilService.findByCondition(bean);
        return LayuiUtil.data(0L, list);
    }

    /**
     * Description: 带条件查询班次时间段记录
     *
     * @param bean
     */
    @RequestMapping("findNumRunDeilByNumRunId")
    @ResponseBody
    public Object findNumRunDeilByNumRunId(NumRunDeilBean bean) {
        List<NumRunDeilBean> list = numRunDeilService.findNumRunDeilByNumRunId(bean);
        return LayuiUtil.data(0L, list);
    }


    //为班次新增时间段
    @RequestMapping("insert")
    @ResponseBody
    @Transactional
    public String insert(NumRunDeilBean bean) throws ParseException {
        NumRunBean numRunBean = new NumRunBean();
        numRunBean.setId(bean.getNumRunId());
        numRunBean.setSchClassId(bean.getSchClassId());
        boolean flag = numRunService.update(numRunBean);
        if (flag) {
//            SchClassBean schClassBean = new SchClassBean();
//            schClassBean.setId(bean.getSchClassId());
//            List<SchClassBean> list = schClassService.findSchClassName(schClassBean);
//            bean.setStartTime(list.get(0).getStartTime());
//            bean.setEndTime(list.get(0).getEndTime());
            boolean flag2 = numRunDeilService.insert(bean);
            if (flag2) {
                return "SUCCESS";
            }
        }
        return "ERROR";
    }

    /**
     * Description: 查询是否存在班次时段记录
     *
     * @param bean
     */
    @RequestMapping("findNumRunDeilBean")
    @ResponseBody
    public Object findNumRunDeilBean(NumRunDeilBean bean) {
        List<NumRunDeilBean> list = numRunDeilService.findNumRunDeilBean(bean);
        return list;
    }

    //清除时段
    @RequestMapping("delete")
    @ResponseBody
    @Transactional
    public String delete(NumRunDeilBean bean) {
        Integer id = bean.getNumRunId();

        boolean flag = numRunDeilService.delete(bean);
        if (flag) {
            boolean flag2 = numRunService.deleteSchClassById(id);
            if (flag2) {
                return "SUCCESS";
            }
        }
        return "ERROR";
    }


    //删除单个时段
    @RequestMapping("deleteOneById")
    @ResponseBody
    @Transactional
    public String deleteOneById(NumRunDeilBean bean) {
        NumRunBean numRunBean = numRunService.findById(bean.getNumRunId());
        boolean flag = numRunDeilService.delete(bean);
        if (flag) {
            // 清除对应时段的排班
            deleteUserOfRunDeil(numRunBean,bean.getsDays());
            return "SUCCESS";
        }
        return "ERROR";
    }

    public void deleteUserOfRunDeil(NumRunBean numRunBean,int sDays){
//        NumRunDeilBean bean
        //周期单位(1周、2天、3月)
        if (numRunBean.getUnits().intValue() == 1) {
            // 防止多周期，先查询是否还存在相同星期的排班
            NumRunDeilBean bean = new NumRunDeilBean();
            bean.setsDays(sDays);
            bean.setNumRunId(numRunBean.getId());
            List<NumRunDeilBean> numRunDeilBean = numRunDeilService.findNumRunDeilBean(bean);
            // 如果不存在 删除对应周的排班
            if (numRunDeilBean.size() == 0) {
                // 取得周
                String week = getWeek((sDays+1)%7);
                userOfRunDeilService.deleteByWeek(week,numRunBean.getId());
            }
        }
        //周期单位(1周、2天、3月)
        if (numRunBean.getUnits().intValue() == 2) {
            sDays += 1;
            // 判断周期数  如果当前删除的天数加上周期数小于一个月 说一月类有多次对应排班
            while (sDays <= 31) {
                String day = sDays<10?"0"+sDays:""+sDays;
                userOfRunDeilService.deleteByDay(day,numRunBean.getId());
                sDays += numRunBean.getCycle().intValue();
            }
        }
        //周期单位(1周、2天、3月)
        if (numRunBean.getUnits().intValue() == 3) {
            sDays += 1;
            String day = sDays<10?"0"+sDays:""+sDays;
            userOfRunDeilService.deleteByDay(day,numRunBean.getId());
        }
    }

    public String getWeek(int week){
        switch (week) {
            case 0:return "Sunday";
            case 1:return "Monday";
            case 2:return "Tuesday";
            case 3:return "Wednesday";
            case 4:return "Thursday";
            case 5:return "Friday";
            case 6:return "Saturday";
        }
        return "";
    }
}
